package com.example.onlineeducation.mapper

import com.example.onlineeducation.data.entity.NoticeEntity
import com.example.onlineeducation.data.json.NoticeListJson
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycToNoEmpty
import com.yc.jetpacklib.mapper.IMapper

class NoticeMapper : IMapper<List<NoticeListJson>?, List<NoticeEntity>?> {
    override fun map(input: List<NoticeListJson>?): List<NoticeEntity> {
        val list = mutableListOf<NoticeEntity>()
        if (input.ycIsNotEmpty()) {
            for (item in input!!) {
                list.add(
                    NoticeEntity(
                        item.name.ycToNoEmpty(),
                        item.url,
                        item.content,
                        item.position.ycToNoEmpty(Int.MAX_VALUE)
                    )
                )
            }
            // 列表排序
            val lengthComparator = Comparator { data1: NoticeEntity, data2: NoticeEntity ->
                data1.position - data2.position
            }
            return list.sortedWith(lengthComparator)
        } else {
            return list
        }

    }
}


